package cn.ljy.uem.web.action.backstage.school;

import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.annotation.LimitRequest;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.common.utils.page.Pagination;
import cn.ljy.uem.model.SchoolTeachingTask;
import cn.ljy.uem.model.SchoolTeachingTaskStudent;
import cn.ljy.uem.model.query.SchoolTeachingTaskQuery;
import cn.ljy.uem.model.query.SchoolTeachingTaskStudentQuery;
import cn.ljy.uem.service.SchoolTeachingTaskService;
import cn.ljy.uem.service.SchoolTeachingTaskStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;


/**
 * @description 教学任务模块的业务层接口
 * @author 廖振豪
 * @date 2022-7-20
 */
@Api(tags = "后台的教学任务模块")
@RestController
@Slf4j
@Validated //加了后@NoNull之类的注解才能生效，如果只是@valid注解则不需要加上
@RequestMapping(value = "/api/backstage/school/schoolteachingtaskstudent")
public class SchoolTeachingTaskStudentController {

    @Autowired
    private SchoolTeachingTaskStudentService baseService;

    @ApiOperation(value = "查询所有教学任务")
    @GetMapping("/all")
    @LimitRequest
    protected ResponseData query(SchoolTeachingTaskStudentQuery query) {
        return ResponseData.success(baseService.findAll(query));
    }

    @ApiOperation(value = "分页查询教学任务列表")
    @LimitRequest
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页最大记录数", defaultValue = "20")
    })
    // 由于本操作不是所有模块都有，一般分页和查询所有记录都是取其一。
    // 所以取消@getmappding，避免因为没写代码被访问到。
    // 子类需要手动添加@GetMapping
    @GetMapping
    public ResponseData query(Integer page, Integer pageSize, SchoolTeachingTaskStudentQuery query){
        Pagination pagination = baseService.findAll(page,pageSize,query);
        return ResponseData.success(pagination);
    }

    @ApiOperation(value = "批量删除指定的多个教学班学生")
    @LimitRequest
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teachingTaskId", value = "学生的教学班", required = true,paramType = "query"),
            @ApiImplicitParam(name = "studentIds", value = "要删除的id集合", required = true,paramType = "query")})
    @DeleteMapping
    protected ResponseData deletes(Integer teachingTaskId, @NotEmpty(message = "操作失败：请选择要移除的记录") Integer[] studentIds) {
        baseService.delete(teachingTaskId,(Serializable[]) studentIds);
        return ResponseData.success(null);
    }


    @ApiOperation(value = "添加教学班学生")
    @LimitRequest
    @PostMapping
    protected ResponseData add(@Valid @RequestBody SchoolTeachingTaskStudent schoolTeachingTaskStudent) {
        baseService.save(schoolTeachingTaskStudent);
        return ResponseData.success(null);
    }

    @ApiOperation(value = "编辑教学班学生类型")
    @LimitRequest
    @PutMapping
    protected ResponseData edit(@Valid @RequestBody SchoolTeachingTaskStudent schoolTeachingTaskStudent) {
        baseService.update(schoolTeachingTaskStudent);
        return ResponseData.success(null);
    }


}
